/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2015-11-11 20:17:00
 * Last Modified: 2017-04-24 20:16:04
 * File Name: controllers/registe.go
 * Description:
 */
package controllers

import (
	"encoding/json"
	"strings"

	"passport/auth"
	"passport/errorcode"
	"passport/register"

	log "github.com/cihub/seelog"
)

type RegisterController struct {
	URLSignController
}

/*
	手机账号注册
*/
func (p *RegisterController) UserMobileRegister() {
	defer p.Output("UserMobileRegister")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &register.MobileRegisterReq{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	reqBody.RegIp = p.Ctx.Input.IP()
	reqBody.RegPlat = auth.GetAuthAppKeyService().GetPlat(p.AppKey)
	reqBody.AKId = auth.GetAuthAppKeyService().GetAKId(p.AppKey)
	reqBody.Vuid = strings.ToLower(p.GetString("vuid", ""))
	reqBody.InviteCode = strings.ToUpper(reqBody.InviteCode)

	code, msg, data := register.GetMobileRegisterService().RegisterAccountForMobile(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if code == errorcode.EC_GL_SUCCESS && data != nil {
		p.Resp["access_token"] = data.AccessToken
		p.Resp["expire_second"] = data.ExpireSecond
		p.Resp["user_id"] = data.Userid
		p.Resp["hx_account"] = data.HXAccount
		p.Resp["hx_passwd"] = data.HXPasswd
	}
}

/*
	手机账号一键注册登录
*/
func (p *RegisterController) QuickRegLogin() {
	defer p.Output("QuickRegLogin")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &register.MobileRegLoginReq{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	reqBody.RegIp = p.Ctx.Input.IP()
	reqBody.RegPlat = auth.GetAuthAppKeyService().GetPlat(p.AppKey)
	reqBody.AKId = auth.GetAuthAppKeyService().GetAKId(p.AppKey)
	code, msg, data := register.GetMobileRegisterService().RegLogin(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if code == errorcode.EC_GL_SUCCESS && data != nil {
		p.Resp["access_token"] = data.AccessToken
		p.Resp["expire_second"] = data.ExpireSecond
		p.Resp["user_id"] = data.Userid
		p.Resp["hx_account"] = data.HXAccount
		p.Resp["hx_passwd"] = data.HXPasswd
	}
}

//web端用户注册
/*
	手机账号注册
*/
func (p *RegisterController) UserMobileRegisterByWeb() {
	defer p.Output("UserMobileRegisterByWeb")

	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &register.MobileRegisterReq{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	reqBody.RegIp = p.Ctx.Input.IP()
	reqBody.RegPlat = auth.GetAuthAppKeyService().GetPlat(p.AppKey)
	reqBody.AKId = auth.GetAuthAppKeyService().GetAKId(p.AppKey)
	reqBody.Vuid = strings.ToLower(p.GetString("vuid", ""))
	reqBody.InviteCode = strings.ToUpper(reqBody.InviteCode)

	code, msg, data := register.GetMobileRegisterService().RegisterAccountForMobileByWeb(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if code == errorcode.EC_GL_SUCCESS && data != nil {
		p.Resp["access_token"] = data.AccessToken
		p.Resp["expire_second"] = data.ExpireSecond
		p.Resp["user_id"] = data.Userid
		p.Resp["hx_account"] = data.HXAccount
		p.Resp["hx_passwd"] = data.HXPasswd
	}
}

/*
	手机账号一键注册登录
*/
func (p *RegisterController) QuickRegLoginByWeb() {
	defer p.Output("QuickRegLoginByWeb")
	body := p.Ctx.Input.RequestBody
	log.Debugf("recv body %s", body)
	reqBody := &register.MobileRegLoginReq{}
	if err := json.Unmarshal(body, reqBody); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	reqBody.RegIp = p.Ctx.Input.IP()
	reqBody.RegPlat = auth.GetAuthAppKeyService().GetPlat(p.AppKey)
	reqBody.AKId = auth.GetAuthAppKeyService().GetAKId(p.AppKey)
	code, msg, data := register.GetMobileRegisterService().RegLoginByWeb(reqBody)
	p.Resp["code"] = code
	p.Resp["msg"] = msg
	if code == errorcode.EC_GL_SUCCESS && data != nil {
		p.Resp["access_token"] = data.AccessToken
		p.Resp["expire_second"] = data.ExpireSecond
		p.Resp["user_id"] = data.Userid
		p.Resp["hx_account"] = data.HXAccount
		p.Resp["hx_passwd"] = data.HXPasswd
	}
}
